package ihm_clarifiee.Presentation.popup;

import ihm_clarifiee.application.Histoire;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JFrame;

import ihm_clarifiee.Base_Ajout_Independante.model_table_backlog;
import ihm_clarifiee.Presentation.model_table.model_table_detailsprint;
import ihm_clarifiee.application.Backlog;
import ihm_clarifiee.application.Sprint;
import ihm_clarifiee.application.Tache;

import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.html.HTMLDocument.Iterator;

/*
 * DetailSprint.java
 *
 * Created on 18 mai 2011, 18:14:11
 */
/**
 *
 * @author Simon
 */
public class DetailSprint extends javax.swing.JFrame implements popup {

    /** Creates new form DetailSprint */
    public DetailSprint(Sprint sp,Backlog bk) {
    	m_sprint = sp;
        m_backlog=bk;
    	initComponents();
        update();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    public void update()
    {
//            java.util.Iterator<Histoire> ithist = m_sprint.getlisteHistoire().iterator();
//            Histoire hist;
//            Tache tache;
//            m_modele_table.removeAll();
//            while(ithist.hasNext())
//            {
//            hist = ithist.next();
//            String [] temp = new String[3];
//            temp[0]=hist.getNom();
//             java.util.Iterator<Tache> ittache = hist.getListTaches().iterator();
//                while(ittache.hasNext())
//                {
//                tache=ittache.next();
//                temp[1]=temp[1]+tache.getTitre()+",";
//                }
//             m_modele_table.add(temp);
//            }
    	m_modele_table.removeAll();
    	for (int i=0; i<m_sprint.getlisteHistoire().size(); i++) {
        	for (int j=0; j<m_sprint.getlisteHistoire().get(i).getListTaches().size(); j++) {
        		String[] ajout = {m_sprint.getlisteHistoire().get(i).getNom(),
        						m_sprint.getlisteHistoire().get(i).getListTaches().get(j).getTitre(),
        						""}; //ici le proprio, TODO
            	m_modele_table.add(ajout);
            	m_array_tache.add(m_sprint.getlisteHistoire().get(i).getListTaches().get(j));
        	}
        }
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        m_table = new javax.swing.JTable();
        m_combo_jour = new javax.swing.JComboBox();
        m_fld_heures = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        m_lbl_nomtache = new javax.swing.JLabel();
        m_bt_confirmer = new javax.swing.JButton();
        m_bt_assigner = new javax.swing.JButton();
        m_bt_ajouter = new javax.swing.JButton();
        m_bt_supprimer = new javax.swing.JButton();
        m_bt_quitter = new javax.swing.JButton();
        m_label_capacite = new javax.swing.JLabel();
        m_array_tache = new ArrayList<Tache>();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(m_sprint.getNom());
        setResizable(false);
        
        m_modele_table = new model_table_detailsprint();
        m_table.setModel(m_modele_table);
        
        jScrollPane1.setViewportView(m_table);
        m_table.getColumnModel().getColumn(0).setPreferredWidth(50);
        m_table.getColumnModel().getColumn(1).setPreferredWidth(250);
        m_table.getColumnModel().getColumn(2).setPreferredWidth(25);

        m_table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				if (m_table.getSelectedRow() != -1) {
					Tache t = m_array_tache.get(m_table.getSelectedRow());
					
					m_lbl_nomtache.setText(t.getTitre());
					m_fld_heures.setText(t.getTpsParJour(m_combo_jour.getSelectedIndex())+"");
				}
			} 
        } );
        
        m_combo_jour.addActionListener(new ActionListener() {
        	public void actionPerformed(java.awt.event.ActionEvent evt) {
        		if (m_table.getSelectedRow() != -1) {
        			Tache t = m_array_tache.get(m_table.getSelectedRow());
            		m_fld_heures.setText(t.getTpsParJour(m_combo_jour.getSelectedIndex())+"");
        		}
            }
        });
        
        
        String[] jours = new String[m_sprint.getDuree()];
        for (int i=0; i<m_sprint.getDuree(); i++) {
        	Date current = new Date(m_sprint.getDateDebut().getTime() + i*86400000);
        	jours[i] = current.toString();
        }
        m_combo_jour.setModel(new javax.swing.DefaultComboBoxModel(jours));

        jLabel1.setText("Nom de la t�che");

        jLabel2.setText("Jour");

        jLabel3.setText("Heures travaill�es");

        m_bt_confirmer.setText("Confirmer");
        m_bt_confirmer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	m_bt_confirmerActionPerformed(evt);
            }
        });
        
        m_bt_assigner.setText("Assigner propri�taire");

        m_bt_ajouter.setText("Ajouter histoire");
        m_bt_ajouter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	m_bt_ajouterActionPerformed(evt);
            }
        });

         m_bt_supprimer.setText("Supprimer histoire");
        m_bt_supprimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	m_bt_supprimerActionPerformed(evt);
            }

            private void m_bt_supprimerActionPerformed(ActionEvent evt) {
             int n = m_table.getSelectedRow();
             if (n != -1) {
            	 for (int i=0; i<m_sprint.getlisteHistoire().size(); i++) {
                  	for (int j=0; j<m_sprint.getlisteHistoire().get(i).getListTaches().size(); j++) {
                  		if (n == 0) {
                  			m_sprint.getlisteHistoire().get(i).associerSprint(null);
                  			m_sprint.getlisteHistoire().remove(i);
                  			update();
                  			break;
                  		}
                  		else {
                  			n--;
                  		}
                  	}
                  }
             }
            }
        });

        m_bt_quitter.setText("Quitter");
         m_bt_quitter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	m_bt_quitterActionPerformed(evt);
            }

            private void m_bt_quitterActionPerformed(ActionEvent evt) {
              dispose();
            }


        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 707, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(m_lbl_nomtache, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(m_combo_jour, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(m_fld_heures, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 105, Short.MAX_VALUE)
                                .addComponent(m_bt_confirmer))
                            .addComponent(jLabel3))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(m_label_capacite, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                    .addComponent(m_bt_assigner, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(m_bt_ajouter, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                    .addComponent(m_bt_supprimer, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                    .addComponent(m_bt_quitter, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(m_bt_confirmer)
                                .addComponent(m_fld_heures, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(m_combo_jour, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(m_lbl_nomtache, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(m_bt_assigner)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(m_bt_ajouter)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(m_bt_supprimer)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(m_bt_quitter)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(m_label_capacite, javax.swing.GroupLayout.DEFAULT_SIZE, 202, Short.MAX_VALUE)))
                .addContainerGap())
        );        
        
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void m_bt_confirmerActionPerformed(java.awt.event.ActionEvent evt) {
        m_array_tache.get(m_table.getSelectedRow()).setTpsParJour(m_combo_jour.getSelectedIndex(), new Integer(m_fld_heures.getText()));
    }
    
    private void m_bt_ajouterActionPerformed(java.awt.event.ActionEvent evt) {  
    	// On cr�e la fen�tre d'ajout, en lui donnant :
    	//		- La table � modifier (partie m�tier)
    	//		- Le composant de l'IHM � modifier
    	JFrame popup = new Popup_Ajout_Histoire(m_backlog.getListeHistoires(), m_sprint,this);
    	popup.setVisible(true);
    } 
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton m_bt_assigner;
    private javax.swing.JButton m_bt_confirmer;
    private javax.swing.JButton m_bt_ajouter;
    private javax.swing.JButton m_bt_supprimer;
    private javax.swing.JButton m_bt_quitter;
    private javax.swing.JComboBox m_combo_jour;
    private javax.swing.JTextField m_fld_heures;
    private javax.swing.JLabel m_label_capacite;
    private javax.swing.JLabel m_lbl_nomtache;
    private javax.swing.JTable m_table;
    private static model_table_detailsprint m_modele_table;
    
    private Sprint m_sprint;
    private Backlog m_backlog;
    private ArrayList<Tache> m_array_tache;

    public boolean valider() {
        update();
        return true;
    }
    // End of variables declaration//GEN-END:variables
}
